package com.happybluefin.tapburstfree.common.sinaweibo;

import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SinaWeibo {
    public SinaWeibo(Activity parent, SinaWeiboCallback callback) {
        Log.d(TAG, "SinaWeibo() start");

        mCallback = callback;
        mParent = parent;

        Log.d(TAG, "SinaWeibo() end");
    }

    public void share(final String text) {
        Log.d(TAG, "share() start");

        Weibo weibo = Weibo.getInstance();
        weibo.setupConsumerConfig(APP_KEY, APP_SECRET);
        weibo.setRedirectUrl(APP_REDIRECT_URL);
        weibo.authorize(mParent, new WeiboDialogListener() {
            @Override
            public void onComplete(Bundle values) {
                Weibo weibo = Weibo.getInstance();
                try {
                    boolean result = weibo.share2weibo(mParent, weibo.getAccessToken().getToken(), weibo.getAccessToken()
                            .getSecret(), text, null);
                    if (mCallback != null) {
                        mCallback.shareResult(result);
                    }
                }
                catch (WeiboException e) {
                    e.printStackTrace();
                    if (mCallback != null) {
                        mCallback.shareResult(false);
                    }
                }
            }

            @Override
            public void onWeiboException(WeiboException e) {
                e.printStackTrace();
                if (mCallback != null) {
                    mCallback.shareResult(false);
                }
            }

            @Override
            public void onError(DialogError e) {
                if (mCallback != null) {
                    mCallback.shareResult(false);
                }
            }

            @Override
            public void onCancel() {
                if (mCallback != null) {
                    mCallback.cancel();
                }
            }
        });

        Log.d(TAG, "share() end");
    }

    protected SinaWeiboCallback mCallback = null;

    protected Activity mParent = null;

    protected static final String APP_KEY = "2527422900";

    protected static final String APP_SECRET = "42472858c47693762b4a3dcfcdeceadf";

    protected static final String APP_REDIRECT_URL = "http://";

    protected static final String TAG = "SinaWeibo";
}